﻿using StackExchange.Redis;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace WebApplication1
{
    public class DataBaseConfig
    {
        #region mysql链接配置

        private static string DefaultSqlConnectionString = @"database=student;data source=127.0.0.1;user id=root;password=ww623025;";
        private static string DefaultRedisString = "localhost, abortConnect=false";
        private static ConnectionMultiplexer redis;

        public static IDbConnection GetSqlConnection(string sqlConnectionString = null)
        {
            if (string.IsNullOrWhiteSpace(sqlConnectionString))
            {
                sqlConnectionString = DefaultSqlConnectionString;
            }
            //配置mysql是MySqlConnection  配置其他数据库下载对应的包就好了，然后改掉这个连接对象
            IDbConnection conn = new MySqlConnection(sqlConnectionString);
            conn.Open();
            return conn;
        }

        #endregion

        #region Redis链接配置

        private static ConnectionMultiplexer GetRedis(string redisString = null)
        {
            if (string.IsNullOrWhiteSpace(redisString))
            {
                redisString = DefaultRedisString;
            }
            if (redis == null || redis.IsConnected)
            {
                redis = ConnectionMultiplexer.Connect(redisString);
            }
            return redis;
        }

        #endregion
    }
}
